package com.qs.javame.thread.future;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

public class TestFuture {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);

//        for (int i = 0; i < 10; i++) {
//            executorService.execute(() -> {
//                System.out.println(Thread.currentThread().getName() + ":ceshi");
//
//                try {
//                    TimeUnit.SECONDS.sleep(3);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
//        }


//        CallTask callTask = new CallTask();
//        for (int i = 0; i < 10; i++) {
//            Future<Integer> resultFuture = executorService.submit(callTask);
//            System.out.println("-->>result=" + resultFuture.get());
//        }
//
//        System.out.println("result=" + callTask.getResult());
//        executorService.shutdown();


        List<String> result = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Future<List<String>> future = executorService.submit(new Callable<List<String>>() {

                @Override
                public List<String> call() throws Exception {
                    return getResult();
                }

                private List<String> getResult() {
                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < 5; i++) {
                        list.add(String.valueOf(new Random().nextInt(100)));
                    }

                    System.out.println(list);
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    return list;
                }
            });

            result.addAll(future.get());
        }


        System.out.println("result=" + result);
        executorService.shutdown();


//        Thread[] threads = new Thread[1000];
//        RunTask runTask = new RunTask();
//        for (int i = 0; i < 1000; i++) {
//            threads[i] = new Thread(runTask);
//            threads[i].start();
//        }
//
//        System.out.println("result=" + runTask.getResult());
    }


    private static class CallTask implements Callable<Integer> {
        private Integer result = 0;

        public Integer getResult() {
            return result;
        }

        @Override
        public synchronized Integer call() throws Exception {
            result += 1;
            return result;
        }
    }


    private static class RunTask implements Runnable {

        private Integer result = 0;

        public Integer getResult() {
            return result;
        }

        @Override
        public synchronized void run() {
            //并非是原子操作，需要加synchronized或者使用AtomicInteger
            result += 1;
        }
    }
}
